{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W1025 21:28:43.693413 17152 deprecation.py:323] From c:\\users\\dell\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "import tensorflow.compat.v1 as tf\n",
    "from train import load_data,get_corrupted\n",
    "tf.disable_v2_behavior()\n",
    "tf.reset_default_graph()\n",
    "entity, relation, G = load_data()\n",
    "dim=50\n",
    "learning_rate=0.01\n",
    "entity = entity\n",
    "relation = relation\n",
    "G = G\n",
    "dim = dim\n",
    "margin=1\n",
    "epochs=100\n",
    "batches=400"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "<tf.Tensor 'loss:0' shape=() dtype=string>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h_pos = tf.placeholder(dtype=tf.int32, name='h_pos', shape=[None])\n",
    "t_pos = tf.placeholder(dtype=tf.int32, name='t_pos', shape=[None])\n",
    "h_neg = tf.placeholder(dtype=tf.int32, name='h_neg', shape=[None])\n",
    "t_neg = tf.placeholder(dtype=tf.int32, name='t_neg', shape=[None])\n",
    "r = tf.placeholder(dtype=tf.int32, name='r_neg', shape=[None])\n",
    "ent_embeddings = tf.get_variable(name=\"ent_embedding\",shape=[len(entity),dim],\n",
    "                                 initializer=tf.random_uniform_initializer(minval=-6/np.sqrt(dim), maxval=6/np.sqrt(dim)))\n",
    "rel_embeddings = tf.Variable(tf.random_uniform(minval=-6/np.sqrt(dim), maxval=6/np.sqrt(dim),shape=[len(relation), dim]),\n",
    "                             name=\"rel_embedding\")\n",
    "h_pos_data=tf.nn.embedding_lookup(ent_embeddings,h_pos)\n",
    "t_pos_data=tf.nn.embedding_lookup(ent_embeddings,t_pos)\n",
    "h_neg_data=tf.nn.embedding_lookup(ent_embeddings,h_neg)\n",
    "t_neg_data=tf.nn.embedding_lookup(ent_embeddings,t_neg)\n",
    "\n",
    "r_data=tf.nn.embedding_lookup(rel_embeddings,r)\n",
    "#使用L2loss，平方版本的L2范数\n",
    "qq=h_pos_data+r_data-t_pos_data\n",
    "# pp=tf.reduce_sum(h_pos+r-t_pods,1)\n",
    "#每一行进行规范化\n",
    "?*50\n",
    "# tf.nn.l2_normalize 这个是对每一个值进行规范化\n",
    "n1=tf.reduce_sum(tf.square(h_pos_data+r_data-t_pos_data),1)\n",
    "n2=tf.reduce_sum(tf.square(h_neg_data+r_data-t_neg_data),1)\n",
    "loss=tf.reduce_sum(n1+n2+margin)\n",
    "optimizer=tf.train.AdamOptimizer(learning_rate)\n",
    "optimizer=optimizer.minimize(loss)\n",
    "tf.summary.histogram('loss',loss)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'numpy.str_' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-12-9e4ffa310057>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m     13\u001B[0m                 \u001B[0mr_data\u001B[0m\u001B[1;33m:\u001B[0m\u001B[0mthis_batch\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     14\u001B[0m                 \u001B[0mt_pos\u001B[0m\u001B[1;33m:\u001B[0m\u001B[0mthis_batch\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m2\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 15\u001B[1;33m                 \u001B[0mh_neg\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0mget_corrupted\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mentity\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0mq\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m0\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mq\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mthis_batch\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     16\u001B[0m                 \u001B[0mt_neg\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0mget_corrupted\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mentity\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0mq\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m2\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mq\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mthis_batch\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     17\u001B[0m             })\n",
      "\u001B[1;32m<ipython-input-12-9e4ffa310057>\u001B[0m in \u001B[0;36m<listcomp>\u001B[1;34m(.0)\u001B[0m\n\u001B[0;32m     13\u001B[0m                 \u001B[0mr_data\u001B[0m\u001B[1;33m:\u001B[0m\u001B[0mthis_batch\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     14\u001B[0m                 \u001B[0mt_pos\u001B[0m\u001B[1;33m:\u001B[0m\u001B[0mthis_batch\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m2\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 15\u001B[1;33m                 \u001B[0mh_neg\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0mget_corrupted\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mentity\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0mq\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m0\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mq\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mthis_batch\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     16\u001B[0m                 \u001B[0mt_neg\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m[\u001B[0m\u001B[0mget_corrupted\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mentity\u001B[0m\u001B[1;33m,\u001B[0m\u001B[0mq\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m2\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mq\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mthis_batch\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     17\u001B[0m             })\n",
      "\u001B[1;32m~\\Desktop\\论文\\对齐\\trans-e\\train.py\u001B[0m in \u001B[0;36mget_corrupted\u001B[1;34m(entity, triple)\u001B[0m\n\u001B[0;32m    117\u001B[0m         \u001B[1;32mwhile\u001B[0m \u001B[0mr\u001B[0m \u001B[1;33m==\u001B[0m \u001B[0mtriple\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m0\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    118\u001B[0m             \u001B[0mr\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mrandom\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mchoice\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlist\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mentity\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mvalues\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 119\u001B[1;33m         \u001B[0mtriple\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m0\u001B[0m\u001B[1;33m]\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mr\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    120\u001B[0m     \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    121\u001B[0m         \u001B[0mr\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mrandom\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mchoice\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlist\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mentity\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mvalues\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mTypeError\u001B[0m: 'numpy.str_' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "start_time = time.time()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    writer=tf.summary.FileWriter('logs/',sess.graph)\n",
    "    merged=tf.summary.merge_all()\n",
    "    batch_size=len(G)//batches\n",
    "    for i in range(epochs):\n",
    "        for batch in range(batches):\n",
    "            this_batch = np.array(random.sample(G, batch_size))\n",
    "            merged,l=sess.run([merged,optimizer,loss],feed_dict={\n",
    "                h_pos:this_batch[:,0],\n",
    "                r:this_batch[:,1],\n",
    "                t_pos:this_batch[:,2],\n",
    "                h_neg:[get_corrupted(entity,q[0]) for q in this_batch],\n",
    "                t_neg:[get_corrupted(entity,q[2]) for q in this_batch],\n",
    "            })\n",
    "        print('epoch {}/{}, use time {:.2f},loss {:.2f}.'.\n",
    "              format(i+1,epochs,time.time()-start_time,l))\n",
    "        test()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "print(tf.config.list_physical_devices()[2])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}